﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.Text.RegularExpressions;

using Common = VietStar.Library.Commom.Module;

public partial class control_client_update_news : System.Web.UI.UserControl
{
    Common.Config cfg = new Common.Config();
    Common.Spider spider = new Common.Spider();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            pcworld.InnerHtml = GetNewsPcworldKapersky(cfg.LinkPcworld, cfg.RegexContentPcworld, 5);
            zing.InnerHtml = GetNewsZing(cfg.LinkZing, cfg.RegexContentZing, 5);
            kapersky.InnerHtml = GetNewsPcworldKapersky(cfg.LinkKapersky, cfg.RegexContentKapersky, 5);
        }        
    }
    //Load tin tức pcworld + kapersky
    private string GetNewsPcworldKapersky(string strLink, string regexContent, int numNews)
    {
        StringBuilder strOutput = new StringBuilder();
        try
        {
            string strHTMLWeb = spider.GetWebContent(strLink);
            Match matchResult = Regex.Match(strHTMLWeb, regexContent);
            if (matchResult.Success)
            {
                int i = 1;
                while (matchResult.Success & i<=numNews)
                {
                    strOutput.Append("<li><a target='_blank' href='").Append(strLink).Append(SplitLink(matchResult.Groups["Link"].ToString())).Append("'>");
                    strOutput.Append(matchResult.Groups["TieuDe"].ToString()).Append("<a/></li>");
                    i = i + 1;
                    matchResult = matchResult.NextMatch();
                }
            }
            else
            {
                strOutput.Append("<li>Không thể kết nối!</li>");
            }            
        }
        catch (Exception ex)
        {
            strOutput.Append("<li>Kết nối vượt quá thời gian quy định</li>");
        }
        return strOutput.ToString();
    }

    //Load tin tức Zing
    private string GetNewsZing(string strLink, string regexContent, int numNews)
    {
        StringBuilder strOutput = new StringBuilder();
        try
        {
            string strHTMLWeb = spider.GetWebContent(strLink);
            Match matchResult = Regex.Match(strHTMLWeb, regexContent);
            if (matchResult.Success)
            {
                int i = 1;
                while (matchResult.Success && i<=numNews)
                {
                    strOutput.Append("<li><a target='_blank' ").Append(matchResult.Groups["Link"].ToString()).Append(">");
                    strOutput.Append(matchResult.Groups["TieuDe"].ToString()).Append("<a/></li>");
                    i = i + 1;
                    matchResult = matchResult.NextMatch();
                }
            }
            else
            {
                strOutput.Append("<li>Không thể kết nối!</li>");
            }
        }
        catch (Exception ex)
        {
            strOutput.Append("<li>Kết nối vượt quá thời gian quy định</li>");
        }
        return strOutput.ToString();
    }
  
    protected string SplitLink(string strLink)
    {
        string strOutput;
        try
        {
            string[] strSlipt = strLink.Split('"');
            strOutput = strSlipt[1].ToString();
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
        return strOutput;
    }
}